#! /bin/sh
# PCP QA Test No. 398
# checks pmcd handling empty $PCP_PMCDCONF_PATH
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.check
. ./common.filter

status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full
signal=$PCP_BINADM_DIR/pmsignal

_terminate()
{
    if [ -n "$pid" ]
    then
	$signal -s TERM $pid
	pid=""
	wait
    fi
}

_cleanup()
{
    _terminate
    _restore_config $PCP_PMCDCONF_PATH
    _restore_config $PCP_PMCDOPTIONS_PATH
    _service pcp restart 2>&1 | _filter_pcp_start
    _wait_for_pmcd
    _wait_for_pmlogger
    $sudo rm -f $tmp.*
}

_check()
{
    # try to provoke a core
    pminfo -v hinv >/dev/null 2>&1
    pmsleep 2.5

    $sudo touch $PCP_PMCDLOG_PATH
    grep -i "dumping to core" $PCP_PMCDLOG_PATH
    sts=$?
    if [ "`echo $PCP_LOG_DIR/core*`" != "$PCP_LOG_DIR/core*" -o "`echo $PCP_LOG_DIR/pmcd/core*`" != "$PCP_LOG_DIR/pmcd/core*" -o $sts -eq 0 ]
    then
	echo "=== core found or logfile claims dumping to core! ==="
	echo '=== Here is $PCP_PMCDLOG_PATH ==='
	cat $PCP_PMCDLOG_PATH
	exit
    else
	echo '--- No core file at $PCP_LOG_DIR/core or $PCP_LOG_DIR/pmcd/core ---'
    fi
}

trap "_cleanup; exit \$status" 0 1 2 3 15

# real QA test starts here
_save_config $PCP_PMCDOPTIONS_PATH
echo "# pmcd.options installed by PCP QA test $seq" >$tmp.options
echo PMCD_WAIT_TIMEOUT=10 >> $tmp.options
echo PMCD_CONNECT_TIMEOUT=10 >> $tmp.options
echo PMCD_REQUEST_TIMEOUT=10 >> $tmp.options
$sudo cp $tmp.options $PCP_PMCDOPTIONS_PATH

# terminate any pmcds and get rid of core file to get to a sane starting place
_service pcp stop >/dev/null 2>&1
_wait_pmcd_end
$sudo rm -f $PCP_LOG_DIR/core* $PCP_LOG_DIR/pmcd/core* $PCP_PMCDLOG_PATH

_check

# empty pmcd.conf
_save_config $PCP_PMCDCONF_PATH
$sudo cp /dev/null $PCP_PMCDCONF_PATH

# should be empty
echo "--- pmcd.conf contents follow ---"
cat $PCP_PMCDCONF_PATH
# Note: need to use $PCP_RC_DIR/* here, not _service pcp so that
# systemd does not get a chance to have a look in and syphon off our
# (expected) error messages.
#
( $sudo $PCP_RC_DIR/pmcd restart; $sudo $PCP_RC_DIR/pmlogger restart ) \
	> $tmp.out 2>&1 &
pid=$!

echo "Waiting for PMCD ..."
pmcd_wait -t 5sec
if [ $? -ne 2 ]
then
    echo "Expected timeout - got status $? from pmcd_wait!"
else
    echo "Connection timed out as expected"
fi

echo "Checking for core file ..."
_check

_terminate
cat $tmp.out >> $seq.full

# success, all done
status=0
exit
